package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.CrmAreaManage;
import com.yu.crm.domain.pojo.CrmManageAttachment;
import com.yu.crm.domain.pojo.PubAttachment;
import com.yu.crm.mapper.CrmAreaManageMapper;
import com.yxzz.common.config.UserCacheManager;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台园区区域管理表 服务实现类
 * </p>
 * @author ct
 * @since 2020-06-22
 */
@Service
public class CrmAreaManageService extends ServiceImpl<CrmAreaManageMapper, CrmAreaManage>{

    @Autowired
    private UserCacheManager userCacheManager;
    @Autowired
    private CrmManageAttachmentService crmManageAttachmentService;

    public IPage<CrmAreaManage> getPage(Page<CrmAreaManage> page) {
        QueryWrapper<CrmAreaManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        return baseMapper.selectPage(page,queryWrapper);
    }

    public List<CrmAreaManage> getList() {
        QueryWrapper<CrmAreaManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        return baseMapper.selectList(queryWrapper);
    }

    public void add(CrmAreaManage crmAreaManage) {
        String userId = userCacheManager.getCurrentUser().getUserId();
        crmAreaManage.setCreateUserId(userId);
        crmAreaManage.setGmtCreate(DateHelpers.now());
        baseMapper.insert(crmAreaManage);

        //处理附件
        if (DataHelpers.isNotEmptyString(crmAreaManage.getAttachmentIds())) {
            String[] split = crmAreaManage.getAttachmentIds().split(",");
            crmManageAttachmentService.saveAttachment(crmAreaManage.getId(),split);
        }
    }

    public void updateArea(CrmAreaManage crmAreaManage) {
        String userId = userCacheManager.getCurrentUser().getUserId();
        crmAreaManage.setUpdateUserId(userId);
        crmAreaManage.setGmtModified(DateHelpers.now());
        baseMapper.updateById(crmAreaManage);

        //先删除后新增
        crmManageAttachmentService.delById(crmAreaManage.getId());
        //处理附件
        if (DataHelpers.isNotEmptyString(crmAreaManage.getAttachmentIds())) {
            String[] split = crmAreaManage.getAttachmentIds().split(",");
            crmManageAttachmentService.saveAttachment(crmAreaManage.getId(),split);
        }
    }

    public CrmAreaManage detail(Long id) {
        CrmAreaManage crmAreaManage = baseMapper.selectById(id);
        List<PubAttachment> attachmentList = crmManageAttachmentService.listById(crmAreaManage.getId());
        crmAreaManage.setAttachmentList(attachmentList);
        return crmAreaManage;
    }

    public void deleteById(Long id) {
        String userId = userCacheManager.getCurrentUser().getUserId();
        CrmAreaManage crmAreaManage = baseMapper.selectById(id);
        if (DataHelpers.isNotEmpty(crmAreaManage)) {
            crmAreaManage.setIsDeleted(1);
            crmAreaManage.setUpdateUserId(userId);
            crmAreaManage.setGmtModified(DateHelpers.now());
            baseMapper.updateById(crmAreaManage);
        }
    }
}
